package com.easylinkin.linkappapi.elasticsearch.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easylinkin.linkappapi.annotation.CommonOperateLogAnnotate;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.elasticsearch.entity.BankAlarmNotice;
import com.easylinkin.linkappapi.elasticsearch.entity.BankEnergyAlarmRule;
import com.easylinkin.linkappapi.elasticsearch.service.IBankAlarmRuleService;
import com.easylinkin.linkappapi.elasticsearch.service.impl.BankAlarmRuleServiceImpl;
import com.easylinkin.linkappapi.operatelog.constant.LogConstant.LogModule;
import io.swagger.annotations.ApiOperation;
import javax.annotation.Resource;
import org.quartz.JobExecutionException;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

@RestController
@RequestMapping("/bankAlarmRule")
public class BankAlarmRuleController {
	
	    @Resource
	    private IBankAlarmRuleService bankAlarmRuleService;
	    @Resource
	    private BankAlarmRuleServiceImpl bankAlarmRuleServiceimpl;
	
	    @CommonOperateLogAnnotate(module = LogModule.BANK_ALARM_RULE, desc = "新建能源告警规则")
	    @PostMapping("/addBankAlarmRule")
	    public RestMessage addLinkappArea(@RequestBody BankEnergyAlarmRule rule) {
	        return RestBuilders.successBuilder().success(bankAlarmRuleService.createRule(rule)).build();
	    }
	    
	    @ApiOperation("通过ID查询")
	    @PostMapping("/getBankAlarmRuleById")
	    public RestMessage getBankAlarmRuleById(@RequestBody BankEnergyAlarmRule rule) {
	    	Assert.notNull(rule, "查询参数 不能为空");
	        Assert.notNull(rule.getId(), "规则Id不能为空");
	    	rule  = bankAlarmRuleService.getById(rule.getId());
	    	return RestBuilders.successBuilder().data(rule).build();
	    }

	    @CommonOperateLogAnnotate(module = LogModule.BANK_ALARM_RULE, desc = "更新规则")
	    @PostMapping("/updateBankAlarmRuleById")
	    public RestMessage updateBankAlarmRuleById(@RequestBody BankEnergyAlarmRule rule) {
	        return RestBuilders.successBuilder().success(bankAlarmRuleService.updateRule(rule)).build();
	    }
	    
	    @CommonOperateLogAnnotate(module = LogModule.BANK_ALARM_RULE, desc = "启用规则")
	    @PostMapping("/resumeBankAlarmRuleById")
	    public RestMessage resumeBankAlarmRuleById(@RequestBody BankEnergyAlarmRule rule) {
	        return RestBuilders.successBuilder().success(bankAlarmRuleService.resumeRule(rule)).build();
	    }
	    
	    @CommonOperateLogAnnotate(module = LogModule.BANK_ALARM_RULE, desc = "暂停规则")
	    @PostMapping("/pauseBankAlarmRuleById")
	    public RestMessage pauseBankAlarmRuleById(@RequestBody BankEnergyAlarmRule rule) {
	        return RestBuilders.successBuilder().success(bankAlarmRuleService.pauseRule(rule)).build();
	    }
	    
	    @ApiOperation("通过数据源查询周和月的规则")
	    @PostMapping("/getListByDataSourceIdAndEnergyKind")
	    public RestMessage getBankAlarmRuleByDataSourceId(@RequestBody BankEnergyAlarmRule rule) {
	    	Assert.notNull(rule, "查询参数 不能为空");
	    	return RestBuilders.successBuilder().data(bankAlarmRuleService.getBankAlarmRuleByDataSourceId(rule)).build();
	    }
	    
	    @ApiOperation("查询某个数据源的告警分页")
	    @PostMapping("/getBankAlarmPageByDataSourceId")
	    public RestMessage ggetAlarmPageByDataSourceId(@RequestBody RequestModel<BankAlarmNotice> requestModel) {
	    	Page page = requestModel.getPage();
	    	
	    	Assert.notNull(requestModel, "查询参数 不能为空");
	    	Assert.notNull(page, "分页信息 不能为空");
	    	Assert.notNull(requestModel.getCustomQueryParams(), "查询参数 不能为空");	
	    	Assert.notNull(requestModel.getCustomQueryParams().getDataSourceId(), "数据源Id不能为空");	
		    IPage<BankAlarmNotice> record = bankAlarmRuleService.getAlarmNoticePage(page, requestModel.getCustomQueryParams());
		    return RestBuilders.successBuilder().data(record).build();
	
	    }
	    
	    
	    
	    @PostMapping("/testAlrmRuleNotice")
	    public RestMessage testAlrmRuleNotice() throws JobExecutionException {
	    	bankAlarmRuleServiceimpl.execute(null);
		    return null;
	
	    }
	   
	    
	    
	    
	   
}
